package com.whmskj.xjlhsc.exception;

import cn.dev33.satoken.exception.NotLoginException;
import cn.hutool.core.date.DateUtil;
import com.whmskj.xjlhbc.core.AjaxResult;
import com.whmskj.xjlhsc.controller.vo.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;

/**
 * @BelongsProject: XJLHSC
 * @BelongsPackage: com.whmskj.xjlhsc
 * @Author: lk
 * @CreateTime: 2025-06-10
 * @Description: TODO
 * @Version: 1.0
 */
@RestControllerAdvice
@Slf4j
@SuppressWarnings("all")
public class GlobalException {

    /**
     * token校验异常
     */
    @ExceptionHandler(NotLoginException.class)
    public AjaxResult handleAccessDeniedException(NotLoginException e, HttpServletRequest request) {
        e.printStackTrace();
        String requestURI = request.getRequestURI();
        log.error("请求地址'{}',token校验失败'{}'", requestURI, e.getMessage());
        return AjaxResult.error(401, e.getMessage());
    }

    /**
     * 请求方式不支持
     */
    @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
    public AjaxResult handleHttpRequestMethodNotSupported(HttpRequestMethodNotSupportedException e,
                                                          HttpServletRequest request) {
        String requestURI = request.getRequestURI();
        log.error("请求地址'{}',不支持'{}'请求", requestURI, e.getMethod());
        return AjaxResult.error(e.getMessage());
    }

    /**
     * 参数校验异常（IllegalArgumentException）
     */
    @ExceptionHandler(IllegalArgumentException.class)
    public AjaxResult handleIllegalArgumentException(IllegalArgumentException e, HttpServletRequest request) {
        String requestURI = request.getRequestURI();
        log.warn("请求地址'{}',参数校验失败: {}", requestURI, e.getMessage());
        return AjaxResult.error(400, e.getMessage());
    }

    /**
     * 拦截未知的运行时异常
     */
    @ExceptionHandler(RuntimeException.class)
    public AjaxResult handleRuntimeException(RuntimeException e, HttpServletRequest request) {
        String requestURI = request.getRequestURI();
        log.error("请求地址'{}',发生未知异常.", requestURI, e);
        return AjaxResult.error(e.getMessage());
    }

    /**
     * 系统异常
     */
    @ExceptionHandler(Exception.class)
    public AjaxResult handleException(Exception e, HttpServletRequest request) {
        String requestURI = request.getRequestURI();
        log.error("请求地址'{}',发生系统异常.", requestURI, e);
        return AjaxResult.error(e.getMessage());
    }

    /**
     * 自定义验证异常
     */
    @ExceptionHandler(BindException.class)
    public AjaxResult handleBindException(BindException e) {
        log.error(e.getMessage(), e);
        String message = e.getAllErrors().get(0).getDefaultMessage();
        return AjaxResult.error(message);
    }

    /**
     * 自定义验证异常
     */
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public Object handleMethodArgumentNotValidException(MethodArgumentNotValidException e) {
        log.error(e.getMessage(), e);
        String message = e.getBindingResult().getFieldError().getDefaultMessage();
        return AjaxResult.error(message);
    }


    public Result error(Throwable e) {
        e.printStackTrace();
        log.error("当前时间:{}=======异常原因:{}", DateUtil.formatDateTime(DateUtil.date()), e.getMessage());
        return Result.error(e.getMessage());
    }

    @ExceptionHandler(Throwable.class)
    public Result handlerReturn(Throwable e) {
        return error(e);
    }


    @ExceptionHandler(BusinessException.class)
    public Result handlerBusinessException(BusinessException e) {
        e.printStackTrace();
        log.error("业务异常:{}", e.getMessage());
        return Result.error(e.getMessage());
    }
}
